﻿using System;
using System.Web.UI;
using System.Web;
using QzEbook.Admin;
using QzEbook.AccessObject;
using System.Text;
using System.Web.UI.WebControls;
using QzEbook.Common.Emum;

namespace QzEbook.BaseView
{
    /// <summary>
    ///  所有管理页面基类.主页判断用户是否登陆;
    /// </summary>
    public class UserPage:CheckPage
    {
        protected NavType navType = NavType.DEFAULT;
        private StringBuilder sb;

        private string itemTemple = "<li><a href=\"{0}\" title=\"{1}\"><b>{1}</b></a></li>";

        public UserPage()
        {
            this.Init += new EventHandler(UserPage_Init);
        }

        void UserPage_Init(object sender, EventArgs e)
        {
            checkUser();
        }


        protected override void builderNav()
        {
            if (this.Master == null)
            {
                return;
            }
            Localize NavBar = this.Master.FindControl("NavBar") as Localize;
            if (NavBar != null)
            {
                NavBar.Text = bulidDefautl().ToString();
            }
        }
        private StringBuilder bulidDefautl()
        {  
            sb = new StringBuilder("<ul id=\"nav\">");
            
            if (navType == NavType.DEFAULT)
            {
                sb.Append(userpaneString);
                ActionNavItem();
            }
            else
            {
                sb.AppendFormat(itemTemple, "../default.xhtml", "首页");
                sb.AppendFormat(userpaneString);
            }

            sb.Append(loginOutString);
            sb.Append("</ul>");
            return sb;
        }

        private void ActionNavItem()
        {
            if (String.IsNullOrEmpty(Request["Bid"]))
            {
                BuildAddBookNavItem();
            }
            else
            {
                BuildEditBookNavItem(int.Parse(Request["Bid"]));
            }
        }

        private void BuildAddBookNavItem()
        {
            sb.AppendFormat(itemTemple, "DefaultSet.xhtml", "预设置");
        }
        private void BuildEditBookNavItem(int bookId)
        {
           
            string[,] urlList = new string[4, 2];

            urlList[0, 0] = "DefaultSet.xhtml";
            urlList[0, 1] = "预设置";

            urlList[1, 0] = "CommentList.xhtml";
            urlList[1, 1] = "评论管理";

            urlList[2, 0] = "PageList.xhtml";
            urlList[2, 1] = "书页内容配置";

            urlList[3, 0] = "ShowView.xhtml";
            urlList[3, 1] = "预览";

            int len = urlList.GetLength(0) - 1;
            for (int i = 0; i <= len; i++)
            {
                sb.AppendFormat("<li><a href=\"{0}?Bid={2}\" title=\"{1}\"><b>{1}</b></a></li>", urlList[i, 0], urlList[i, 1], bookId);
            }
            sb.ToString();
        }


        private string userpaneString
        {
            get
            {
                 return string.Format(itemTemple, "UserPane.xhtml", "我的面板");
            }
        }

        private string loginOutString
        {
            get
            {
                return string.Format(itemTemple, "Login.xhtml?logout=1", "退出");
            }
        }


    }
}